//
//  NUpLoadChange.m
//  News
//
//  Created by zhangtao on 2018/11/27.
//  Copyright © 2018 zt.td. All rights reserved.
//

#import "NUpLoadChange.h"
#import "NHTTPManage.h"

@implementation NUpLoadChange


+(NSDictionary *)uploadImage:(UIImage *)image{

//    UIImage *image = [UIImage imageNamed:@"littleRight"];
    NSData *imageData = [[NHTTPManage shareInstance] reSizeImage:image maxSizeWithKB:1024];
//    NSString *imgStr = [[UIImage imageWithData: imageData] base64];
    NSString *imgStr = [image base64];
    NSString * strAll = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSString * nonce = [[NSMutableString alloc]initWithCapacity:16];
    for (int i = 0; i < 16; i++)
    {
        NSInteger index = arc4random() % (strAll.length-1);
        char tempStr = [strAll characterAtIndex:index];
        nonce = (NSMutableString *)[nonce stringByAppendingString:[NSString stringWithFormat:@"%c",tempStr]];
    }
    nonce = [nonce uppercaseString];
    
    NSDate *datenow = [NSDate date];
    NSString *tickets = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    
    NSString *nonce16 = [[self hexStringFromString:nonce] uppercaseString];
    NSString *token16 = [[self hexStringFromString:TOKEN] uppercaseString];
    NSString *tickest16 = [[self hexStringFromString:tickets] uppercaseString];
    NSString *uid16 = [[self hexStringFromString:CUID] uppercaseString];
    
    NSString *di = [NSString stringWithFormat:@"appid=EA7D74C5BC4C4DCB9DCA379E24E46D06&appkey=50FD0CA011094C6783A2F518E9D00577&nonce=%@&tickets=%@&token=%@&uid=%@",nonce,tickets,TOKEN,CUID];// @"appid=EA7D74C5BC4C4DCB9DCA379E24E46D06&appkey=50FD0CA011094C6783A2F518E9D00577&nonce=0A982EI9JF93&tickets=636779886890149443&token=7d4aff66fd4845b68b5dc310be571b21&uid=2350453901246857216";
    //[NSString stringWithFormat:@"appid=EA7D74C5BC4C4DCB9DCA379E24E46D06&appkey=50FD0CA011094C6783A2F518E9D00577&nonce=%@&tickets=%@&token=%@&uid=%@",nonce ,tickets,@"7d4aff66fd4845b68b5dc310be571b21",@"2350453901246857216"];
    //    NSString *di = [NSString stringWithFormat:@"uid=%@&token=%@&tickets=%@&nonce=%@",uid16,token16,tickest16,nonce16];
    
    NSString *md5 = [[ACEPublicTool md5:di] uppercaseString];
    md5 = [md5 stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *sh = [[AWFileHash sha1HashOfData:[md5 dataUsingEncoding:NSUTF8StringEncoding]] uppercaseString];
    NSLog(@"...");
    sh = [sh stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSDictionary *dict = @{@"token":TOKEN,@"uid":CUID,@"nonce":nonce,@"tickets":tickets,@"sign":sh,@"imgData":imgStr};
    
    
    return dict;
}



+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

@end
